<?php

/**
 * This is the model class for table "comments".
 *
 * The followings are the available columns in table 'comments':
 * @property integer $id
 * @property integer $user_id
 * @property string $content
 * @property integer $parent_id
 * @property integer $post_id
 * @property integer $status
 * @property string $create_at
 * @property integer $highlight_user
 * @property integer $highlight_mod
 *
 * The followings are the available model relations:
 * @property Announcement $post
 * @property Users $user
 * @property CommentsBase $parent
 * @property CommentsBase[] $comments
 */
class CommentsBase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CommentsBase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'comments';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('post_id, create_at', 'required'),
			array('user_id, parent_id, post_id, status, highlight_user, highlight_mod', 'numerical', 'integerOnly'=>true),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, content, parent_id, post_id, status, create_at, highlight_user, highlight_mod', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'post' => array(self::BELONGS_TO, 'Announcement', 'post_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'parent' => array(self::BELONGS_TO, 'CommentsBase', 'parent_id'),
			'comments' => array(self::HAS_MANY, 'CommentsBase', 'parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'content' => 'Content',
			'parent_id' => 'Parent',
			'post_id' => 'Post',
			'status' => 'Status',
			'create_at' => 'Create At',
			'highlight_user' => 'Highlight User',
			'highlight_mod' => 'Highlight Mod',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('post_id',$this->post_id);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_at',$this->create_at,true);
		$criteria->compare('highlight_user',$this->highlight_user);
		$criteria->compare('highlight_mod',$this->highlight_mod);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}